import VueRouter from "vue-router";
// 引入组件
// import ClassroomInfos from '../components/ClassroomInfos'
// import Table from '../components/Table'
// import User from '../components/User'
// import Login from '../components/Login'
// import Container from '../components/Container'
// import Register from '../components/Register'
// import Logout from '../components/Logout'
// import Analysis from '../components/Analysis'
// import Logout from '../components/Logout'
// 创建一个路由器
// import axios from 'axios'
const router = new VueRouter({
  mode: "history",
  routes: [
    {
      path: "/container",
      name: "container",
      component: () => import("../components/Container.vue"),
      children: [
        {
          path: "classroom",
          name: "classroom",
          components: {
            table: () => import("../components/ClassroomInfos.vue"),
          },
        },
        {
          path: "RoomUsageTable",
          name: "RoomUsageTable",
          components: {
            table: () => import("../components/RoomUsageTable.vue"),
          },
        },
        {
          path: "courseInfos",
          name: "courseInfos",
          components: {
            table: () => import("../components/CourseInfos.vue"),
          },
        },
        {
          path: "classroomTable",
          name: "classroomTable",
          components: {
            table: () => import("../components/ClassroomTable.vue"),
          },
        },
        {
          path: "userInfo",
          name: "userInfo",
          components: {
            table: () => import("../components/UserInfos.vue"),
          },
        },
      ],
    },
    {
      path: "/login",
      component: () => import("../components/Login.vue"),
      name: "login",
    },
    {
      path: "/",
      component: () => import("../components/Login.vue"),
      name: "login",
    },
    {
      path: "/register",
      component: () => import("../components/Register.vue"),
      name: "register",
    },
    // {
    //     path: "classroom",
    //     name: "classroom",
    //     component:  () => import('../components/ClassroomInfos.vue'),

    // },
    // // {
    //     path: "/analysis",
    //     component: Analysis,
    //     name: "analysis",
    // }
    // {
    //     path: "/logout",
    //     component: Logout,
    //     name: "logout"
    // }
  ],
});
// 挂载路由导航守卫
// to 将要访问的路径
// from 代表从哪个路径跳转而来
// next 是个函数，表示放行 next() 放行  next('/login') 强制跳转
router.beforeEach((to, from, next) => {
  // 获取登录状态
  const isLoggedIn = Boolean(window.sessionStorage.getItem("token"));

  // 判断是否访问登录页面
  if (to.path === "/" || to.path === "/login" || to.path === "/register") {
    // 如果已登录，则重定向到首页
    if (isLoggedIn) {
      next("/container/classroom");
    } else {
      next();
    }
  } else {
    // 如果未登录，则重定向到登录页面
    if (!isLoggedIn) {
      next("/login");
    } else {
      next();
    }
  }
});

export default router;
